package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.CollectDao;
import person.aoao.library.entity.Collect;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.CollectService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Collect)表服务实现类
 *
 * @author makejava
 * @since 2023-05-15 21:52:19
 */
@Service("collectService")
public class CollectServiceImpl implements CollectService {
    @Resource
    private CollectDao collectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param collectid 主键
     * @return 实例对象
     */
    @Override
    public Collect queryById(Integer collectid) {
        return this.collectDao.queryById(collectid);
    }

    /**
     * 分页查询
     *
     * @param collect     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Collect> queryByPage(Collect collect, PageRequest pageRequest) {
        long total = this.collectDao.count(collect);
        return new PageImpl<>(this.collectDao.queryAllByLimit(collect, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public Collect insert(Collect collect) {
        Collect data=this.collectDao.isCollect(collect.getUid(),collect.getBookid());
        System.out.println(data);
        if(data!=null){
              return null;
        }
        this.collectDao.insert(collect);
       return collect;
    }

    /**
     * 修改数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public Collect update(Collect collect) {
        this.collectDao.update(collect);
        return this.queryById(collect.getCollectid());
    }

    /**
     * 通过主键删除数据
     *
     * @param collectid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer collectid) {
        return this.collectDao.deleteById(collectid) > 0;
    }

    @Override
    public ResponseData getCollectList(String uid, Integer num, Integer page) {
        ListData data=new ListData();
        List<Collect> list=this.collectDao.getCollectList(uid,num,(page-1)*num);
        long count=this.collectDao.getCount(uid);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData isCollect(String uid, Integer bookid) {
        Collect collect=this.collectDao.isCollect(uid,bookid);
        if(collect!=null){
            return new ResponseData(ResponseCode.SUCCESS,collect);
        }
        return new ResponseData(ResponseCode.SUCCESS,false);
    }
}
